
<template>
  <el-dialog :title="title" :visible="visible" width="630px" @close="closeFn" :modal="false">
    <el-form
      ref="partnerForm"
      v-if="type !== 'preview'"
      :model="partnerForm"
      :rules="rules"
    >
      <el-form-item label="合作商名称：" :label-width="'140px'" prop="name">
        <el-input
          autocomplete="off"
          placeholder="请输入"
          maxlength="10"
          show-word-limit
          :validate-event="false"
          v-model="partnerForm.name"
        ></el-input>
      </el-form-item>
      <el-form-item label="联系人：" :label-width="'140px'" prop="contact">
        <el-input
          autocomplete="off"
          placeholder="请输入"
          maxlength="10"
          show-word-limit
          :validate-event="false"
          v-model="partnerForm.contact"
        ></el-input>
      </el-form-item>
      <el-form-item label="联系电话：" :label-width="'140px'" prop="mobile">
        <el-input
          autocomplete="off"
          placeholder="请输入"
          maxlength="11"
          show-word-limit
          :validate-event="false"
          v-model="partnerForm.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item label="分成比例(%)：" :label-width="'140px'" prop="ratio">
        <el-input-number style="width: 396px" v-model="partnerForm.ratio" controls-position="right" :min="0" :max="100" placeholder="请输入"></el-input-number>
      </el-form-item>
      <el-form-item label="账号：" :label-width="'140px'" prop="account" v-if="type === 'add'">
        <el-input
          autocomplete="off"
          placeholder="请输入"
          maxlength="18"
          show-word-limit
          :validate-event="false"
          v-model="partnerForm.account"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码：" :label-width="'140px'" prop="password" v-if="type === 'add'">
        <el-input
          autocomplete="off"
          placeholder="请输入"
          maxlength="20"
          show-word-limit
          :validate-event="false"
          v-model="partnerForm.password"
        ></el-input>
      </el-form-item>
    </el-form>
    <template v-if="type === 'preview'">
         <el-row>
          <el-col :span="12">
            <el-form>
              <el-form-item label="合作商名称：" :label-width="'140px'">
                <span>{{currentRow.name}}</span>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="12">
            <el-form>
              <el-form-item label="联系人：" :label-width="'140px'">
                <span>{{currentRow.contact}}</span>
              </el-form-item>
            </el-form>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="12">
            <el-form>
              <el-form-item label="联系电话：" :label-width="'140px'">
                <span>{{currentRow.mobile}}</span>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="12">
            <el-form>
              <el-form-item label="分成比例：" :label-width="'140px'">
                <span>{{currentRow.ratio+'%'}}</span>
              </el-form-item>
            </el-form>
          </el-col>
         </el-row>
    </template>
    <div slot="footer" class="dialog-footer" v-if="type !== 'preview'">
      <el-button @click="closeFn" class="cancel">取消</el-button>
      <el-button type="primary" @click="submit">确认</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { addPartner, updatePartner } from '@/api/dianwei'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    type: {
      type: String,
      required: true
    },
    loading: {
      type: Boolean,
      default: false
    },
    partnerList: {
      type: Array,
      required: true
    },
    currentRow: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      partnerForm: {
        name: '',
        account: '',
        password: '',
        ratio: '',
        contact: '',
        mobile: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入合作商名称' },
          {
            validator: (rule, value, callback) => {
              let isRepeat = false
              if (this.partnerForm.id) {
                isRepeat = this.partnerList.filter(item => item.id !== this.partnerForm.id).some(item => item.name === value)
              } else {
                isRepeat = this.partnerList.some(item => item.name === value)
              }
              isRepeat ? callback(new Promise(this.$message.error('已存在相同的合作商名称'))) : callback()
            }
          }
        ],
        contact: [
          { required: true, message: '请输入联系人' }
        ],
        mobile: [
          { required: true, message: '请输入联系电话' },
          { pattern: /^1\d{10}$/, message: '手机号必须是11位纯数字' }
        ],
        ratio: [
          { required: true, message: '请输入分成比例' }
        ],
        account: [
          { required: true, message: '请输入账号' }
        ],
        password: [
          { required: true, message: '请输入密码' }
        ]
      }
    }
  },
  created () {

  },
  computed: {
    title () {
      let text = null
      if (this.type === 'preview') {
        text = '合作商详情'
      } else if (this.type === 'add') {
        text = '新增合作商'
      } else if (this.type === 'edit') {
        text = '修改合作商'
      }
      return text
    }
  },
  methods: {
    closeFn () {
      if (this.type !== 'preview') {
        this.partnerForm = {
          name: '',
          account: '',
          password: '',
          ratio: '',
          contact: '',
          mobile: ''
        }
        this.$refs.partnerForm.resetFields()
      }
      this.$emit('update:visible', false)
    },
    async submit () {
      try {
        await this.$refs.partnerForm.validate()
        if (this.partnerForm.id) {
          console.log(this.partnerForm.id)
          await updatePartner(this.partnerForm)
          this.$message.success('修改成功')
        } else {
          await addPartner(this.partnerForm)
          this.$message.success('新增成功')
        }
        this.closeFn()
        this.$parent.getPartner()
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
* {
  box-sizing: border-box;
}
::v-deep .el-dialog {
  border-radius: 10px;
}
::v-deep .el-dialog__header {
  padding: 20px;
  padding-bottom: 10px;
  .el-dialog__title {
    line-height: 22px !important;
    font-size: 16px !important;
    font-weight: 600;
  }
}
::v-deep .el-dialog__body {
  padding: 20px 20px 30px;
  color: #666;
  .el-form-item {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
    .el-form-item__content {
      line-height: 36px;
      width: 396px;
      .el-input .el-input__inner {
        height: 36px;
        line-height: 36px;
      }
      .el-textarea__inner {
        height: 82px;
      }
    }
  }
}
::v-deep .el-dialog__footer {
  padding-top: 0;
  padding-bottom: 40px;
  .dialog-footer {
    text-align: center;
    .el-button {
      padding: 0;
      // height: 100%;
      width: 80px;
      background: linear-gradient(135deg, #f28a59, #e85c31);
      border: none;
      line-height: 36px;
      color: white;
      margin-left: 34px;
      font-size: 14px;
      &.cancel {
        background: #fbf4f0 !important;
        color: #655b56 !important;
        margin-left: 0px;
      }
    }
  }
}
.el-cascader {
  width: 396px;
}
.el-table{
  margin:0 20px
}
::v-deep .el-table__header-wrapper {
  .el-table__cell {
    color: #666666;
    height: 42px;
    padding: 0 !important;
    font-weight: 400;
    border-bottom: none !important;
  }
}
::v-deep .el-table__body-wrapper {
  .el-table__cell {
    color: #666666;
    height: 44px;
    padding: 10px 0 !important;
    font-weight: 400;
    // border-bottom:none !important
  }
}
</style>
<style>
.el-cascader-menu {
  height: 180px;
  width: 100px;
}
</style>
